﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

///This Screen will be the Options Screen. For now, I'm using it as a menu navigation test, but later it'll change different... stuff in the game.
///resolution, sound, etc.
namespace MyImaginaryFriends_Redux.Screens
{
    class TitleOptionsMenu
    {
        #region Variables

        private State OptionsMenuState;

        private Texture2D t2d_Background;

        private SpriteFont sf_OptionsScreenFont;

        private string s_Selection1, s_Selection2, s_Return;

        private Color c_Selected, c_Unselected;

        private float f_SelectedScale, f_UnselectedScale;

        private int i_Selected;

        private SoundEffect se_MenuScroll;
        private SoundEffect se_MenuSelect;

        private SoundEffectInstance sei_MenuScroll;
        private SoundEffectInstance sei_MenuSelect;

        #endregion

        //CONSTRUCT AND INITIALIZE!!!
        public TitleOptionsMenu()
        {
            OptionsMenuState = State.Hidden;//hide at first, since we're looking at the title screen.

            s_Selection1 = "Selection1";
            s_Selection2 = "Selection2";
            s_Return = "Return to Title";

            c_Selected = Color.Red;
            c_Unselected = Color.White;

            f_SelectedScale = 2.0f;
            f_UnselectedScale = 1.0f;

            i_Selected = 0;
        }

        public State GetOptionsMenuState()
        {
            return OptionsMenuState;
        }

        public void SetOptionsMenuState(State In_State)
        {
            OptionsMenuState = In_State;
        }

        public void LoadContent(ContentManager content)
        {
            t2d_Background = content.Load<Texture2D>("MenuContent/TitleOptionsMenuBG");
            sf_OptionsScreenFont = content.Load<SpriteFont>("MenuContent/Arial");//will change to it's own font, maybe.

            se_MenuScroll = content.Load<SoundEffect>("Audio/SoundEffects/MenuScroll");
            se_MenuSelect = content.Load<SoundEffect>("Audio/SoundEffects/MenuSelect");

            sei_MenuScroll = se_MenuScroll.CreateInstance();
            sei_MenuSelect = se_MenuSelect.CreateInstance();
        }

        public void Update(GameTime gameTime, InputHandler input, Screens.MainMenu titleScreen, Screens.GameplayScreen GameScreen)
        {
            //this will handle the selection process for the options.
            if (OptionsMenuState == State.Active)
            {
                switch (i_Selected)
                {
                    case 0:
                        //no selection is covered.
                        if (input.GetKBStateSingle(Keys.Down) || input.GetGPStateSingle(Buttons.DPadDown))
                        {
                            sei_MenuScroll.Play();
                            i_Selected = 1;//move to select start game
                        } 
                        break;

                    case 1: //Selection 1 is Selected
                        if (input.GetKBStateSingle(Keys.Down) || input.GetGPStateSingle(Buttons.DPadDown))
                        {
                            sei_MenuScroll.Play();
                            i_Selected = 2;//move to select Selection 2
                        }
                        
                        if (input.GetKBStateSingle(Keys.Enter) || input.GetGPStateSingle(Buttons.A))
                        {
                            sei_MenuSelect.Play();
                        }
                        break;

                    case 2://Selection 2 is Selected
                        if (input.GetKBStateSingle(Keys.Down) || input.GetGPStateSingle(Buttons.DPadDown))
                        {
                            sei_MenuScroll.Play();
                            i_Selected = 3;//move to select Return
                        }

                        if (input.GetKBStateSingle(Keys.Up) || input.GetGPStateSingle(Buttons.DPadUp))
                        {
                            sei_MenuScroll.Play();
                            i_Selected = 1;//move back to select Selection 1
                        }
                        if (input.GetKBStateSingle(Keys.Enter) || input.GetGPStateSingle(Buttons.A))
                        {
                            sei_MenuSelect.Play();
                        }
                        break;

                    case 3://Return is Selected
                        if (input.GetKBStateSingle(Keys.Up) || input.GetGPStateSingle(Buttons.DPadUp))
                        {
                            sei_MenuScroll.Play();
                            i_Selected = 2;//move back to select Selection 2
                        }
                        
                        if (input.GetKBStateSingle(Keys.Enter) || input.GetGPStateSingle(Buttons.A))
                        {
                            sei_MenuSelect.Play();

                            if (titleScreen.GetMainMenuState() == State.Hidden)
                                titleScreen.SetMainMenuState(State.Active);

                            OptionsMenuState = State.Hidden;
                        }
                        break;

                }//end switch
            }//end if
        }

        public void Draw(SpriteBatch spriteBatch, Screens.MainMenu titleScreen)
        {
            if (OptionsMenuState == State.Active)
            {
                spriteBatch.Begin();

                switch (i_Selected)
                {
                    case 1:
                        spriteBatch.DrawString(sf_OptionsScreenFont, s_Selection1, new Vector2(100, 100), c_Selected, 0.0f, Vector2.Zero, f_SelectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_OptionsScreenFont, s_Selection2, new Vector2(100, 200), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_OptionsScreenFont, s_Return, new Vector2(100, 300), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        break;

                    case 2:
                        spriteBatch.DrawString(sf_OptionsScreenFont, s_Selection1, new Vector2(100, 100), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_OptionsScreenFont, s_Selection2, new Vector2(100, 200), c_Selected, 0.0f, Vector2.Zero, f_SelectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_OptionsScreenFont, s_Return, new Vector2(100, 300), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        break;

                    case 3:
                        spriteBatch.DrawString(sf_OptionsScreenFont, s_Selection1, new Vector2(100, 100), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_OptionsScreenFont, s_Selection2, new Vector2(100, 200), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_OptionsScreenFont, s_Return, new Vector2(100, 300), c_Selected, 0.0f, Vector2.Zero, f_SelectedScale, SpriteEffects.None, 1.0f);
                        break;

                    default:
                        spriteBatch.DrawString(sf_OptionsScreenFont, s_Selection1, new Vector2(100, 100), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_OptionsScreenFont, s_Selection2, new Vector2(100, 200), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_OptionsScreenFont, s_Return, new Vector2(100, 300), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        break;
                }

                spriteBatch.DrawString(sf_OptionsScreenFont, "OP:"+OptionsMenuState.ToString(), new Vector2(spriteBatch.GraphicsDevice.Viewport.X, spriteBatch.GraphicsDevice.Viewport.Y), c_Unselected * 0.5f, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                spriteBatch.DrawString(sf_OptionsScreenFont, "TI:"+titleScreen.GetMainMenuState().ToString(), new Vector2(spriteBatch.GraphicsDevice.Viewport.X, spriteBatch.GraphicsDevice.Viewport.Y + 20), c_Unselected * 0.5f, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                spriteBatch.DrawString(sf_OptionsScreenFont, i_Selected.ToString(), new Vector2(spriteBatch.GraphicsDevice.Viewport.X, spriteBatch.GraphicsDevice.Viewport.Y + 40), c_Unselected * 0.5f, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                spriteBatch.End();
            }
         
        }

    }
}
